package com.mcml.space;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class MotaRPG extends JavaPlugin implements Listener {
    public static String ItemLoreMotaRPGName;
    public static String ItemLorePhysicsAttack;
    public static String ItemLorePhysicsProtect;
    public static String ItemLoreMagicAttack;
    public static String ItemLoreMagicProtect;
    public static String ItemLoreCombie;
    public static String ItemLoreCrit;
    public static String ItemLoreStrong;

    @Override
    public void onEnable() {
        this.LoadConfig();
        Bukkit.getPluginManager().registerEvents(new DamageResulter(), this);
        this.getLogger().info("Hello!");
    }

    @Override
    public void onDisable() {
        this.getLogger().info("Goodbye!");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        try {
            if (label.equalsIgnoreCase("mrpg")) {
                sender.sendMessage("§b---------------§aMotaRPG v1.3b§b---------------");
                if (args.length == 0) {
                    sender.sendMessage("§c请输入 /mrpg help 来查阅更多内容。");
                    return true;
                } else {
                    if (args[0].equalsIgnoreCase("help")) {
                        sender.sendMessage("§e/mrpg reload 重载插件");
                        sender.sendMessage("§e/mrpg lore add <文字内容> 把这行lore添加到手中的武器。");
                        sender.sendMessage("§e/mrpg lore set <行数> <文字内容> 修改指定行数的lore。");
                        sender.sendMessage("§e/mrpg lore remove <行数> 删除指定行数的lore。");
                        sender.sendMessage("§e/mrpg lore clear 清空该武器的所有lore。");
                    }

                    if (args[0].equalsIgnoreCase("reload")) {
                        if (args.length == 2 && args[1].equalsIgnoreCase("confirm")) {
                            PluginManager pluginManager = this.getServer().getPluginManager();
                            pluginManager.disablePlugin(this);
                            pluginManager.enablePlugin(this);
                            sender.sendMessage("§a重载完成！");
                        } else {
                            sender.sendMessage("重载可能会带来很多问题！请输入mrpg reload confirm来确认！");
                        }
                    }

                    if (args[0].equalsIgnoreCase("lore")) {
                        Player player;
                        ItemStack item;
                        ItemMeta itemMeta;
                        Object lores;
                        if (args[1].equalsIgnoreCase("add")) {
                            player = (Player) sender;
                            item = player.getItemInHand();
                            itemMeta = item.getItemMeta();
                            lores = itemMeta.getLore();
                            if (lores == null) {
                                lores = new ArrayList();
                            }
                            String tmp = "";
                            for (int i = 2; i < args.length; i++) {
                                tmp = tmp + args[i];
                            }
                            tmp = tmp.replaceAll("&", "§");
                            ((List) lores).add(tmp);
                            itemMeta.setLore((List) lores);
                            item.setItemMeta(itemMeta);
                            sender.sendMessage("§a成功添加该物品的Lore！");
                        }

                        if (args[1].equalsIgnoreCase("set")) {
                            player = (Player) sender;
                            item = player.getItemInHand();
                            itemMeta = item.getItemMeta();
                            lores = itemMeta.getLore();
                            if (lores == null) {
                                lores = new ArrayList();
                            }
                            String tmp = "";
                            for (int i = 3; i < args.length; i++) {
                                tmp = tmp + args[i];
                            }
                            tmp = tmp.replaceAll("&", "§");
                            ((List) lores).set(Integer.parseInt(args[2]) - 1, tmp);
                            itemMeta.setLore((List) lores);
                            item.setItemMeta(itemMeta);
                            sender.sendMessage("§e成功修改该物品的Lore！");
                        }

                        if (args[1].equalsIgnoreCase("remove")) {
                            player = (Player) sender;
                            item = player.getItemInHand();
                            itemMeta = item.getItemMeta();
                            lores = itemMeta.getLore();
                            if (lores == null) {
                                lores = new ArrayList();
                            }

                            ((List) lores).remove(Integer.parseInt(args[2]) - 1);
                            itemMeta.setLore((List) lores);
                            item.setItemMeta(itemMeta);
                            sender.sendMessage("§c成功删除该物品的Lore！");
                        }

                        if (args[1].equalsIgnoreCase("clear")) {
                            player = (Player) sender;
                            item = player.getItemInHand();
                            itemMeta = item.getItemMeta();
                            List<String> tlores = new ArrayList();
                            itemMeta.setLore(tlores);
                            item.setItemMeta(itemMeta);
                            sender.sendMessage("§c成功清空该物品的Lore！");
                        }
                    }
                    return true;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            sender.sendMessage("§c命令语法错误！请输入 /mrpg help 来查阅语法。");
            return false;
        }
    }

    public void LoadConfig() {
        File ConfigFile = new File(this.getDataFolder(), "config.yml");
        if (!ConfigFile.exists()) {
            try {
                ConfigFile.createNewFile();
            } catch (IOException var5) {
            }
        }

        YamlConfiguration config = YamlConfiguration.loadConfiguration(ConfigFile);
        if (config.getInt("Version") != 100) {
            config.set("Version", 100);
            config.set("ItemLore.MotaRPGName", "已经强化");
            config.set("ItemLore.PhysicsAttack", "物理攻击");
            config.set("ItemLore.PhysicsProtect", "物理防御");
            config.set("ItemLore.MagicAttack", "魔法攻击");
            config.set("ItemLore.MagicProtect", "魔法防御");
            config.set("ItemLore.Combie", "连击");
            config.set("ItemLore.Crit", "暴击率");
            config.set("ItemLore.Strong", "坚固");
        }

        ItemLoreMotaRPGName = config.getString("ItemLore.MotaRPGName");
        ItemLorePhysicsAttack = config.getString("ItemLore.PhysicsAttack");
        ItemLorePhysicsProtect = config.getString("ItemLore.PhysicsProtect");
        ItemLoreMagicAttack = config.getString("ItemLore.MagicAttack");
        ItemLoreMagicProtect = config.getString("ItemLore.MagicProtect");
        ItemLoreCombie = config.getString("ItemLore.Combie");
        ItemLoreCrit = config.getString("ItemLore.Crit");
        ItemLoreStrong = config.getString("ItemLore.Strong");

        try {
            config.save(ConfigFile);
        } catch (IOException var4) {
        }

    }
}